package com.example.myapplication.ui.dashboard;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.myapplication.BleGATT.ClientUtils;
import com.example.myapplication.Entity.BlueToothAdapter;
import com.example.myapplication.R;
import com.example.myapplication.RunActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@SuppressLint("MissingPermission")
public class DevicesFragment extends Fragment {
    private static List<BluetoothDevice> MyBlueDevices; // 蓝牙设备列表
    private RunActivity mActivity;  // 扫描活动
    private ClientUtils clientUtils; // 蓝牙客户端工具

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof RunActivity) {
            mActivity = (RunActivity) context; // 获取扫描活动的引用
            clientUtils = mActivity.getClientUtils(); // 获取蓝牙客户端工具的引用
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyBlueDevices = new ArrayList<>(); // 初始化蓝牙设备列表
        MyBlueDevices=mActivity.getBluetoothDevice();
        // 使用 HashSet 过滤掉重复的设备
        Set<BluetoothDevice> uniqueDevices = new HashSet<>(MyBlueDevices);
        // 将过滤后的设备重新放回原始列表中
        MyBlueDevices.clear();
        MyBlueDevices.addAll(uniqueDevices);
        sortDevices(MyBlueDevices);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, container, false);
        ListView mListView = view.findViewById(R.id.ble_detils_list);

        // 获取蓝牙设备列表
        if (!MyBlueDevices.isEmpty()) {
            Toast.makeText(getActivity(), "新增" + MyBlueDevices.size() + "个设备", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "没有扫描到蓝牙设备", Toast.LENGTH_LONG).show();
        }
        BlueToothAdapter adapter = new BlueToothAdapter(getContext(), MyBlueDevices);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener((parent, view1, position, id) -> {
            // 处理点击事件
            Object clickedItem = parent.getItemAtPosition(position);
            if (clickedItem != null) {
                BluetoothDevice bluetoothDevice = (BluetoothDevice) clickedItem;
                if (mActivity != null && clientUtils != null) {
                    clientUtils.connectDevice(bluetoothDevice);
                }
            }
        });
        return view;
    }


    /** 有名字的排前面
     * @param devices
     */
    public static void sortDevices(List<BluetoothDevice> devices) {
        Collections.sort(devices, (device1, device2) -> {
            String name1 = device1.getName();
            String name2 = device2.getName();

            if (name1 == null && name2 != null) {
                return 1;
            } else if (name1 != null && name2 == null) {
                return -1;
            }

            if (name1 != null && name2 != null) {
                return name1.compareTo(name2);
            }
            return 0;
        });
    }
}
